home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cpptut22.zip
/
CHAP01.TXT
< prev
next >
Wrap
Text File
|
1992-01-20
|
23KB
|
531 lines
Chapter 1
SIMPLE THINGS
As we begin the study of C++ and object oriented programming, a few
comments are in order to help you get started. Since the field of
object oriented programming is probably new to you, you will find
that there is a significant amount of new terminology for you to
grasp. This is true of any new endeavor and you should be warned
not to be intimidated by all of the new concepts. We will add a
few new topics in each chapter and you will slowly grasp the entire
language.
Chapters one through four of this tutorial will concentrate on the
non object oriented programming additions to C++. We will not
begin the discussion of any object oriented programming techniques
until chapter five.
EVEN COMMENTS ARE IMPROVED IN C++
_________________________________________________________________
Examine the file named CONCOM.CPP for an example ==============
of several new things in C++. We will take the CONCOM.CPP
new constructs one at a time beginning with the ==============
comments.
A comment begins with the double slash "//", starts anywhere on a
line, and runs to the end of that line where it is automatically
terminated. The old method of comment definition used with ANSI-
C can also be used with C++ as illustrated in lines 11 through 14,
among other places in this program. The new method is the
preferred method of comment definition because it is impossible to
inadvertently comment out several lines of code. This can be done
by forgetting to include the end of comment notation when using the
older C method of comment notation. Good programming practice
would be to use the new method for all comments and reserve the old
method for use in commenting out a section of code during debugging
since the two methods can be nested.
It would be well to caution you at this point however, that you
should not use comments when the same sense of program definition
can be obtained by using meaningful names for variables, constants,
and functions. The careful selection of variable and function
names can make nearly any code self documenting and you should
strive to achieve this in your code.
Page 1-1
Chapter 1 - Simple Things
THE KEYWORDS const AND volatile
_________________________________________________________________
There are two new keywords used in lines 9 through 11 which were
not part of the original K&R definition of C, but are part of the
ANSI-C standard. The keyword const is used to define a constant.
In line 9 the constant is of type int, it is named START, and is
initialized to the value 3. The compiler will not allow you to
accidentally or purposefully change the value of START because it
has been declared a constant. If you had another variable named
STARTS, the system would not allow you to slightly misspell STARTS
as START and accidentally change it. The compiler would give you
an error message so you could fix the error. Since it is not
permissible to change the value of a constant, it is imperative
that you initialize it when it is declared so it will have a useful
value. The compiler does not require you to initialize it however,
and will not issue an error message if you do not.
You will note that the keyword const is also used in the function
header in line 21 to indicate that the formal parameter named
data_value is a constant throughout the function. Any attempt to
assign a new value to this variable will result in a compile error.
This is a small thing you can add to your programs to improve the
compilers ability to detect errors for you.
The keyword volatile is also part of the ANSI-C standard but was
not included in the original K&R definition of C. Even though the
value of a volatile variable can be changed by you, the programmer,
there may be another mechanism by which the value could be changed,
such as by an interrupt timer causing the value to be incremented.
The compiler needs to know that this value may be changed by some
external force when it optimizes the code. A study of code
optimization methods is very interesting, but beyond the scope of
this tutorial. Note that a constant can also be volatile, which
means that you cannot change it, but the system can through some
hardware function.
Ignore the output statement given in line 23 for a few minutes.
We will study it in some detail later in this chapter. If you are
experienced in K&R style programming, you may find line 5 and 21
a little strange. This illustrates prototyping and the modern
method of function definition as defined by the ANSI-C standard.
We will discuss this in great detail in chapter 4 of this tutorial.
Prototyping is optional in C but absolutely required in C++. For
that reason, chapter 4 of this tutorial is devoted entirely to
prototyping.
It would be advantageous for you to compile and execute this
program with your C++ compiler to see if you get the same result
as given in the comments at the end of the listing. One of the
primary purposes of compiling it is to prove that your compiler is
loaded and executing properly.
Page 1-2
Chapter 1 - Simple Things
THE SCOPE OPERATOR
_________________________________________________________________
The example program named SCOPEOP.CPP ===============
illustrates another construct that is new to SCOPEOP.CPP
C++. There is no corresponding construct in ===============
either K&R or ANSI-C. This allows access to the
global variable named index even though there is
a local variable of the same name within the main function. The
use of the double colon in front of the variable name, in lines 11,
13, and 16, instructs the system that we are interested in using
the global variable named index, defined in line 4, rather than the
local variable defined in line 8.
The use of this technique allows access to the global variable for
any use. It could be used in calculations, as a function
parameter, or for any other purpose. It is not really good
programming practice to abuse this construct, because it could make
the code difficult to read. It would be best to use a different
variable name instead of reusing this name, but the construct is
available to you if you find that you need it sometime.
The scope operator allows access to global variables even though
hidden by a local variable. Be sure to compile and execute this
program before proceeding on to the next example program where we
will discuss the cout operator in lines 10, 11, 15, and 16.
THE iostream LIBRARY
_________________________________________________________________
Examine the example program named MESSAGE.CPP ===============
for our first hint of object oriented MESSAGE.CPP
programming, even though it is a very simple ===============
one. In this program, we define a few variables
and assign values to them for use in the output
statements illustrated in lines 17 through 20, and in lines 23
through 26. The new operator cout is the output function to the
standard device, the monitor, but works a little differently from
our old familiar printf() function, because we do not have to tell
the system what type we are outputting.
C++, like the C language itself, has no input or output operations
as part of the language itself, but defines the stream library to
add input and output functions in a very elegant manner.
The operator <<, sometimes called the "put to" operator but more
properly called the insertion operator, tells the system to output
the variable or constant following it, but lets the system decide
how to output the data. In line 17, we first tell the system to
output the string, which it does by copying characters to the
monitor, then we tell it to output the value of index. Notice
however, that we fail to tell it what the type is or how to output
the value. Since we don't tell the system what the type is, it is
Page 1-3
Chapter 1 - Simple Things
up to the system to